<template>
	<view class="container exc-log-container">
		<view class="page-top">
			<view class="page-top-title">我的积分：</view>
			<view class="page-top-main">
				<view class="page-top-num">{{myCoin}}</view>
			</view>
		</view>
		<view class="page-main" v-if="pageList.length>0">
			<view class="page-main-title">积分明细</view>
			<view class="page-list" :class="item.unUse&&'unUse'" v-for="item in pageList">
				<view class="list-top">
					<image class="list-img" :src="$fileUrl+'coupon.png'" mode="aspectFill"></image>
					<view class="list-right">
						<view class="list-title">{{item.description}}</view>
						<view class="list-date">日期：{{$format(item.createTime)}}</view>
						<view class="list-num" :class="item.points<0&&'c-success'">{{item.points}}积分</view>
					</view>
				</view>
				<!-- <view class="list-bottom">
					<view class="yuan-btn btn-primary-opacity" v-if="!item.unUse">去使用</view>
					<view class="yuan-btn btn-primary" v-else>已过期</view>
				</view> -->
			</view>

		</view>
		<view class="not-more" v-if="isLoading">加载中...</view>
		<view class="null-page" v-else-if="pageList.length<=0 ">
			<text class="iconfont">&#xe642;</text>
			<view class="null-page-tips">暂无数据~</view>
		</view>
		<view class="not-more" v-else-if="notMore">没有更多了...</view>
	</view>
</template>

<script setup lang="ts">
	import { onMounted, ref } from 'vue';
	import { onPullDownRefresh, onReachBottom } from "@dcloudio/uni-app"
	import { hideLoading, showLoading } from '../../common/util';
	import userApi from '../../common/userApi';
	const isLoading = ref(false)
	const notMore = ref(true)
	const pageSize = 10
	let pageNum = 1
	const pageList = ref([])
	const loadMore = () => {
		if (isLoading.value) return
		if (notMore.value) return
		pageNum = pageNum + 1
		getPageList()
	}
	const refreshList = () => {
		notMore.value = false
		pageNum = 1
		pageList.value = []
		getPageList()
	}
	onReachBottom(() => {
		loadMore()
	})
	onPullDownRefresh(() => {
		refreshList()
		uni.stopPullDownRefresh()
	})
	const getPageList = async () => {
		showLoading()
		isLoading.value = true
		try {
			const params = { pageNum, pageSize }
			const res = await userApi.getConversionRecord()
			const data = res.data?.recordList || []
			if (data.length < pageSize) notMore.value = true
			pageList.value = pageList.value.concat(data)

		} finally {
			isLoading.value = false
			hideLoading()
		}
	}
	onMounted(async () => {
		getPageList()
		getMyCoin()
	})
	const myCoin = ref(0)
	const getMyCoin = async () => {
		showLoading()
		try {
			const res = await userApi.getUserPoints()
			myCoin.value = res.data.userPoints
		} finally {
			isLoading.value = false
			hideLoading()
		}
	}
</script>

<style lang="scss">
	.exc-log-container {
		background-color: #F2F3F8;
		padding: 25rpx;
		padding-bottom: 120rpx;

		.page-top-title {
			font-size: 24rpx;
			color: #202020;
			font-weight: bold;
		}

		.page-top-main {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20rpx 0;
		}

		.page-top-num {
			font-size: 84rpx;
			color: #3AD3C1;
			font-weight: bold;
		}

		.page-main {
			background-color: #fff;
			padding: 20rpx;
			border-radius: 10rpx;
		}

		.page-main-title {
			font-size: 30rpx;
			color: #222222;
			font-weight: bold;
			margin-bottom: 20rpx;
		}

		.page-list {
			background-color: rgba(242, 243, 248, 1);
			border-radius: 10rpx;
			padding: 30rpx;
			margin-bottom: 20rpx;
		}

		.unUse {
			filter: grayscale(100%);
		}

		.list-top {
			display: flex;
			align-items: center;
		}

		.list-img {
			height: 150rpx;
			width: 150rpx;
			display: block;
			flex-shrink: 0;
			background-color: #F7F7F7;
		}

		.list-right {
			margin-left: 30rpx;
		}

		.list-title {
			font-size: 32rpx;
			color: #202020;
			font-weight: bold;
		}

		.list-date {
			font-size: 24rpx;
			color: #383838;
			margin: 15rpx 0;
		}

		.list-num {
			font-size: 24rpx;
			color: #FF645D;
		}

		.list-bottom {
			border-top: solid 1px rgba(232, 232, 232, 1);
			padding-top: 30rpx;
			margin-top: 20rpx;
			text-align: right;

			.yuan-btn {
				display: inline-flex;
				width: 200rpx;
				height: 60rpx;
				border-radius: 60rpx;
				font-size: 24rpx;
			}
		}
	}
</style>